package com.farm.learn.service.impl;

import com.farm.learn.domain.ClassHour;
import com.farm.learn.domain.Playtime;
import com.farm.learn.domain.ex.PlayInfo;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;

import com.farm.learn.dao.ClasshourDaoInter;
import com.farm.learn.dao.PlaytimeDaoInter;
import com.farm.learn.service.PlaytimeServiceInter;
import com.farm.parameter.FarmParameterService;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：课件播放时常服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class PlaytimeServiceImpl implements PlaytimeServiceInter {
	@Resource
	private PlaytimeDaoInter playtimeDaoImpl;
	@Resource
	private ClasshourDaoInter classhourDaoImpl;
	private static final Logger log = Logger.getLogger(PlaytimeServiceImpl.class);

	@Override
	@Transactional
	public void deletePlaytimeEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		playtimeDaoImpl.deleteEntity(playtimeDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public DataQuery createPlaytimeSimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query,
				"WLP_U_PLAYTIME a left join ALONE_AUTH_USER b on a.userkey=b.id left join WLP_L_CLASSHOUR c on c.id=a.hourid left join WLP_L_CLASS d on d.id =c.classid",
				"a.ID as ID,a.DURATION as DURATION,a.HOURID as HOURID,a.PTIME as PTIME,a.USERKEY as USERKEY,a.LTIME as LTIME,b.name as USERNAME,c.TITLE as HOURTITLE,d.name as CLASSNAME");
		return dbQuery;
	}

	@Override
	@Transactional
	public void loadPlayCache() {
		synchronized (PlaytimeServiceImpl.class) {
			for (String infokey : playingCache.keySet()) {
				try {
					PlayInfo info = playingCache.get(infokey);
					ClassHour hour = classhourDaoImpl.getEntity(info.getHourid());
					List<Playtime> plays = playtimeDaoImpl
							.selectEntitys(DBRuleList.getInstance().add(new DBRule("USERKEY", info.getUserkey(), "="))
									.add(new DBRule("HOURID", info.getHourid(), "=")).toList());
					if (plays.size() > 0) {
						// 更新
						Playtime entity = playtimeDaoImpl.getEntity(plays.get(0).getId());
						// entity.setHourid(info.getHourid());
						// entity.setLtime(info.getDaytime8());
						entity.setPtime(playTimeFilter(entity.getPtime() + info.getAllPlayMinute(), hour.getAltime()));
						entity.setDuration(info.getCurentProcess());
						entity.setLtime(info.getDaytime8());
						// entity.setUserkey(info.getUserkey());
						playtimeDaoImpl.editEntity(entity);
					} else {
						// 添加
						Playtime entity = new Playtime();
						entity.setHourid(info.getHourid());
						entity.setLtime(info.getDaytime8());
						entity.setDuration(info.getCurentProcess());
						entity.setPtime(playTimeFilter(info.getAllPlayMinute(), hour.getAltime()));
						entity.setUserkey(info.getUserkey());
						entity.setClassid(hour.getClassid());
						playtimeDaoImpl.insertEntity(entity);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			playingCache.clear();
		}
	}

	/**
	 * 过滤学习时间，是否允许超过课时时间
	 * 
	 * @param timeMinute
	 * @return
	 */
	private int playTimeFilter(int timeMinute, int hourAltime) {
		if (FarmParameterService.getInstance().getParameterBoolean("config.playing.hourtime.limit.is")) {
			if (timeMinute > hourAltime) {
				timeMinute = hourAltime;
			}
		}
		return timeMinute;
	}

	@Override
	@Transactional
	public int getClassLeanTime(String classid, String userid, int maxtime) {
		int timeMinute = playtimeDaoImpl.getClassLeanTime(classid, userid);
		if (FarmParameterService.getInstance().getParameterBoolean("config.playing.hourtime.limit.is")) {
			if (timeMinute > maxtime) {
				timeMinute = maxtime;
			}
		}
		return timeMinute;
	}

	@Override
	@Transactional
	public Playtime getHourPlayTime(String hourid, String userid) {
		List<DBRule> rules = new ArrayList<DBRule>();
		rules.add(new DBRule("HOURID", hourid, "="));
		rules.add(new DBRule("USERKEY", userid, "="));
		List<Playtime> playTimes = playtimeDaoImpl.selectEntitys(rules);
		if (playTimes.size() > 0) {
			return playTimes.get(0);
		}
		return null;
	}
}
